package com.orientation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orientation.commom.exception.CustomException;
import com.orientation.entity.ArticleCate;
import com.orientation.service.ArticleCateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2021-11-23
 */
@Api(tags = "类别管理")
@RestController
@RequestMapping("/cate")
public class ArticleCateController {
    @Resource
    private ArticleCateService articleCateService;

    @ApiOperation("添加")
    @PostMapping
    public boolean save(@RequestBody ArticleCate article) {
        article.setFixed(0);
        return articleCateService.save(article);
    }

    @ApiOperation("修改")
    @PutMapping
    public boolean update(@RequestBody ArticleCate article) {
        if (article.getFixed().equals(1)) {
            throw new CustomException("10301", "固定字段不可修改");
        }
        article.setFixed(0);
        return articleCateService.updateById(article);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        ArticleCate article = articleCateService.getById(id);
        if (article.getFixed().equals(1)) {
            throw new CustomException("10301", "固定字段不可删除");
        }
        return articleCateService.removeById(id);
    }

    @ApiOperation("查询单条")
    @GetMapping("/{id}")
    public ArticleCate findById(@PathVariable String id) {
        return articleCateService.getById(id);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public List<ArticleCate> findAll() {
        return articleCateService.list();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public IPage<ArticleCate> findPage(@RequestParam(required = false) String name,
                                       @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                       @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return articleCateService.page(new Page<>(pageNum, pageSize), Wrappers.<ArticleCate>lambdaQuery().like(StringUtils.isNotBlank(name), ArticleCate::getName, name));
    }



}


